import 'package:flower/components/custom_toast.dart';
import 'package:flower/config/service_url.dart';
import 'package:flower/model/base_model.dart';
import 'package:flower/model/user_info.dart';
import 'package:flower/pages/login_page.dart';
import 'package:flower/pages/tabs/user/children/collection.dart';
import 'package:flower/pages/tabs/user/children/comment.dart';
import 'package:flower/pages/tabs/user/children/history.dart';
import 'package:flower/pages/tabs/user/children/order.dart';
import 'package:flower/pages/tabs/user/children/personal.dart';
import 'package:flower/pages/tabs/user/children/setting.dart';
import 'package:flower/utils/my_colors.dart';
import 'package:flower/utils/my_icons.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

import '../../../components/cached_image_widget.dart';
import '../../../utils/http_util.dart';
import 'children/address.dart';

class UserPage extends StatefulWidget {
  const UserPage({super.key});

  @override
  State<UserPage> createState() => _UserPageState();
}

class _UserPageState extends State<UserPage> {
  int couponCount = 0;
  // 没登录时的默认数据
  UserInfoData userInfoData = UserInfoData(
      id: 1,
      username: "liuhaha",
      password: "",
      phone: "17318615767",
      email: "4568799@qq.com",
      nickname: "刘哈哈",
      point: 0,
      avatar: "1.jpg",
      gender: 0,
      description: "这个人很懒~",
      vip: 3,
      role: 1,
      createDate: "2024-2-1");

  @override
  void initState() {
    super.initState();
    queryMemberInfo();
    queryCouponCount();
  }

  // 请求用户个人信息
  void queryMemberInfo() async {
    Response result = await HttpUtil.get(getPersonalInfo);
    UserInfoModel userInfoModel = UserInfoModel.fromJson(result.data);
    setState(() {
      userInfoData = userInfoModel.data!;
    });
  }

  // 查询可用优惠券数量
  void queryCouponCount() async {
    Response result = await HttpUtil.get(getCouponCount);
    BaseModel baseModel = BaseModel.fromJson(result.data);
    setState(() {
      couponCount = baseModel.data!;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Color(int.parse('f5f5f5', radix: 16)).withAlpha(255),
        width: MediaQuery.of(context).size.width,
        child: CustomScrollView(
          shrinkWrap: true,
          slivers: [
            buildHeader(),
            buildIntegral(),
            buildOrderInfo(),
            buildSetting()
          ],
        ),
      ),
    );
  }

  SliverPadding buildHeader() {
    return SliverPadding(
        padding: const EdgeInsets.all(0),
        sliver: SliverList(
          delegate: SliverChildListDelegate(<Widget>[
            Stack(
              children: [
                SizedBox(
                  width: MediaQuery.of(context).size.width,
                  child: Image.asset(
                    "assets/images/user_bg.png",
                    height: 200,
                    // width: MediaQuery.of(context).size.width,
                    fit: BoxFit.cover,
                  ),
                ),
                Container(
                  decoration: BoxDecoration(
                      color: MyColors.description,
                      borderRadius: BorderRadius.circular(5)),
                  height: 70,
                  width: 220,
                  padding: const EdgeInsets.all(10),
                  margin: const EdgeInsets.only(top: 118, left: 94, right: 10),
                  child: Text(
                    userInfoData.description,
                    style: const TextStyle(fontSize: 15, color: Colors.white),
                  ),
                ),
                Container(
                  height: 200,
                  margin: const EdgeInsets.symmetric(horizontal: 15),
                  child: Row(
                    children: [
                      InkWell(
                        onTap: () {
                          Navigator.of(context).push(
                            MaterialPageRoute(
                              builder: (context) => const Personal(),
                            ),
                          );
                        },
                        child: ClipOval(
                          child: CachedImageWidget(
                            80,
                            80,
                            userInfoData.avatar,
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                      const SizedBox(
                        width: 10,
                      ),
                      Text(userInfoData.nickname,
                          style: TextStyle(
                              fontSize: 25,
                              color: Color(int.parse('303133', radix: 16))
                                  .withAlpha(255))),
                    ],
                  ),
                ),
                Container(
                  margin: const EdgeInsets.all(20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      InkWell(
                        onTap: () {
                          // Navigator.of(context).push(
                          //   MaterialPageRoute(
                          //     builder: (context) => const LoginPage(),
                          //   ),
                          // );
                          CustomToast.showWarningToast("该功能正在开发中~");
                        },
                        child: Image.asset(
                          "assets/images/message.png",
                          height: 30,
                          width: 30,
                        ),
                      ),
                      const SizedBox(
                        width: 15,
                      ),
                      InkWell(
                        onTap: () {
                          Navigator.of(context).push(
                            MaterialPageRoute(
                              builder: (context) => const Settings(),
                            ),
                          );
                        },
                        child: Image.asset(
                          "assets/images/setting_white.png",
                          height: 30,
                          width: 30,
                        ),
                      ),
                    ],
                  ),
                )
              ],
            )
          ]),
        ));
  }

  SliverPadding buildIntegral() {
    var numStyle = TextStyle(
        fontSize: 16,
        color: Color(int.parse('303133', radix: 16)).withAlpha(255));
    var txtStyle = TextStyle(
        fontSize: 12,
        color: Color(int.parse('707070', radix: 16)).withAlpha(255));
    return SliverPadding(
        padding: const EdgeInsets.all(0),
        sliver: SliverList(
          delegate: SliverChildListDelegate(<Widget>[
            Container(
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(5)),
              height: 70,
              margin: const EdgeInsets.only(top: 8, left: 10, right: 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  InkWell(
                    onTap: () {
                      Navigator.pushNamed(context, "/point");
                    },
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(userInfoData.point.toString(), style: numStyle),
                        Text("积分", style: txtStyle),
                      ],
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      Navigator.pushNamed(context, "/coupon");
                    },
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(couponCount.toString(), style: numStyle),
                        Text("优惠券", style: txtStyle),
                      ],
                    ),
                  ),
                ],
              ),
            )
          ]),
        ));
  }

  SliverPadding buildOrderInfo() {
    var style = TextStyle(
        fontSize: 12,
        color: Color(int.parse('303133', radix: 16)).withAlpha(255));
    return SliverPadding(
        padding: const EdgeInsets.all(0),
        sliver: SliverList(
          delegate: SliverChildListDelegate(<Widget>[
            InkWell(
              onTap: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => const OrderList(),
                  ),
                );
              },
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(5)),
                margin: const EdgeInsets.only(top: 8, left: 10, right: 10),
                height: 88,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Icon(MyIcons.allOrders,
                            size: 20, color: MyColors.girlBlue),
                        const SizedBox(
                          height: 5,
                        ),
                        Text("全部订单", style: style),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Icon(MyIcons.waitPay,
                            size: 20, color: MyColors.girlBlue),
                        const SizedBox(
                          height: 5,
                        ),
                        Text("待付款", style: style),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Icon(MyIcons.waitDelivery,
                            size: 20, color: MyColors.girlBlue),
                        const SizedBox(
                          height: 5,
                        ),
                        Text("待发货", style: style),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Icon(MyIcons.waitReceive,
                            size: 20, color: MyColors.girlBlue),
                        const SizedBox(
                          height: 5,
                        ),
                        Text("待收货", style: style),
                      ],
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Icon(MyIcons.allOrders,
                            size: 20, color: MyColors.girlBlue),
                        const SizedBox(
                          height: 5,
                        ),
                        Text("退款/售后", style: style),
                      ],
                    )
                  ],
                ),
              ),
            ),
          ]),
        ));
  }

  SliverPadding buildSetting() {
    var list = ['地址管理', '我的足迹', '我的收藏', '我的评价', '设置'];
    var listImg = [
      'assets/images/address.png',
      'assets/images/history.png',
      'assets/images/shoucang.png',
      'assets/images/pingjia.png',
      'assets/images/setting.png'
    ];
    var click = [
      const Address(),
      const History(),
      const Collection(),
      const Comment(),
      const Settings()
    ];

    var border = BorderSide(
        width: 1, color: Color(int.parse('f5f5f5', radix: 16)).withAlpha(255));
    var boxDecoration = BoxDecoration(
      color: Colors.white,
      border: Border(bottom: border),
    );

    return SliverPadding(
        padding: const EdgeInsets.all(0),
        sliver: SliverList(
          delegate: SliverChildListDelegate(<Widget>[
            Container(
              margin: const EdgeInsets.only(top: 8, left: 10, right: 10),
              padding: const EdgeInsets.symmetric(horizontal: 10),
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(5)),
              height: 350,
              child: MediaQuery.removeViewPadding(
                  removeTop: true,
                  context: context,
                  child: ListView.builder(
                      itemCount: list.length,
                      itemBuilder: (BuildContext context, int index) {
                        return InkWell(
                          onTap: () {
                            Navigator.of(context).push(
                              MaterialPageRoute(
                                builder: (context) => click[index],
                              ),
                            );
                          },
                          child: Container(
                            decoration: boxDecoration,
                            padding: const EdgeInsets.symmetric(horizontal: 15),
                            height: 50,
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Row(
                                  children: [
                                    Expanded(
                                        child: Row(
                                      children: [
                                        Image.asset(
                                          listImg[index],
                                          height: 20,
                                          width: 20,
                                        ),
                                        const SizedBox(
                                          width: 10,
                                        ),
                                        Text(list[index],
                                            style: TextStyle(
                                                fontSize: 14,
                                                color: Color(int.parse('303133',
                                                        radix: 16))
                                                    .withAlpha(255))),
                                      ],
                                    )),
                                    Image.asset(
                                      "assets/images/right_arrow.png",
                                      height: 15,
                                      width: 15,
                                    )
                                  ],
                                )
                              ],
                            ),
                          ),
                        );
                      })),
            )
          ]),
        ));
  }
}
